package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/demo")
    @ResponseBody
    public String demo(){

        return "框架整合初步完成";
    }

    @RequestMapping("/userList")
    public String userList(Model model){
        List<User> userList = userService.findAll();
        model.addAttribute("userList",userList);
        return "userList";
    }

    @RequestMapping("/user/{name}/{age}/{sex}")
    public String insertUser( User user){
        userService.insertUser(user);
        return "redirect:/userList";
    }

    @RequestMapping("/user/{id}/{name}")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/userList";
    }

//    @RequestMapping("/user/{id}")
//    public String deleteById(@PathVariable Integer id){
//        userService.deleteById(id);
//        return "redirect:/userList";
//    }

    @RequestMapping("/user/{age}")
    public String selectByAge(@PathVariable Integer age,Model model){
        List<User> userList = userService.selectByAge(age);
        model.addAttribute("userList",userList);
        return "userList";
    }

    @RequestMapping("/userAjax")
    public String userAjax(){

        return "userAjax";//转到userAjax页面后，Ajax继续发出get（/findAjaxUser）请求，继而继续来到controller执行业务
    }

    @RequestMapping("/findAjaxUser")
    @ResponseBody
    public List<User> findAjaxUser(){

        return userService.findAll();
    }
}
